package com.xjzd.std.concurrency;

import java.util.concurrent.*;

public class Pool2 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                1,
                5,
                6000,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );

        // FutureTask<String> task = new FutureTask<>(() -> {
        //     Thread.sleep(5000);
        //     return Thread.currentThread().getName() + " hello";
        // });

        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return Thread.currentThread().getName() + " hello";
        }, pool);

        System.out.println(future.join());
        System.out.println(future.get());
        System.out.println("main over");
        pool.shutdown();
    }
}
